﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;

namespace dotNails.CodeGen.Util {
	public class TemplateUtil {
		public static string TransformSimpleTemplateToString(string templateRoot, string templateFile, Dictionary<string, string> substitutions, Assembly assembly) {
			string templatePath = templateRoot + "." + templateFile;
			Stream stream = assembly.GetManifestResourceStream(templatePath);
			string fileContent = string.Empty;

			byte[] buffer = new byte[stream.Length];
			stream.Read(buffer, 0, Convert.ToInt32(stream.Length));
			fileContent = Encoding.ASCII.GetString(buffer, 3, buffer.Length - 3);

			// Insert substitutions
			foreach (string key in substitutions.Keys) {
				fileContent = fileContent.Replace(key, substitutions[key]);
			}
			return fileContent;
		}
	}
}
